Most modern programming languages use static (also called “lexical”) scope rules for associating uses of identifiers, or variables, with their declarations. In a statically scoped language, the scope of a variable is fixed at compile time to some region in the source code containing the variable's declaration. This means that an identifier is only accessible within that region, including procedures declared within the region. Static scoping has been widely implemented because it is easy for programmers to use, helps catch errors at compile time, and because it can be implemented efficiently.
Another type of variable scoping is dynamic scope. Using dynamic scope the scope of the variable depends on the nesting of procedure and function calls at run time. While most modern languages use static scope, some popular languages, including PERL, TCL, TEX, and POSTSCRIPT, offer dynamic scope because it is useful in certain situations. For instance, dynamic scope is particularly useful in applications and libraries that operate in a customizable environment. Full-featured graphical user interface (“GUI”) packages are good examples. These packages support a plethora of style and presentation features that must be programmed in some way. Even many non-GUI applications have some of these characteristics that make dynamic variables useful. For example, compilers often have options that permit clients to specify target machines, compiler passes, linker options, and other features. Moreover, compilers in integrated development environments typically have more options because their clients are other programs. Dynamically scoped variables make access to these features and options very easy.
Without inherent support within a programming language for dynamically scoped variables, such as within a statically scoped language, programming these kinds of features must be done explicitly in terms of the implementation language. Therefore, programmers typically simulate dynamic scope to implement these kinds of features in statically scoped languages. Usually, objects are loaded up with fields or methods that implement these features, or methods and functions are utilized that have a large number of parameters. In extreme cases, programmers move the many fields or parameters into an object, which is passed as a parameter nearly everywhere. Methods are called to read, write, save, and restore these data.
While programming dynamically scoped variables explicitly in a statically scoped programming language provides some of the benefits of dynamic scoping, this approach is not without its drawbacks. In particular, many of these implementations are unstructured, often not type-safe and inefficient in many cases. Moreover, this approach is not easy to use in that it requires that a programmer to explicitly create support for dynamically scoped variables themselves. Additionally, the use of objects with a large number of parameters can be confusing for programmers to use.
Therefore, in light of the above, there is a need for a method and apparatus for providing language constructs for introducing and referencing dynamically scoped variables within an otherwise statically scoped programming language.